package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.NormalFileMessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.chat.VideoMessageBody;
import com.easemob.chat.VoiceMessageBody;
import com.easemob.util.DateUtils;
import com.easemob.util.ImageUtils;
import com.easemob.util.TextFormater;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.application.App;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.ImageViewpager;
import com.example.zbh.dsliao.db.entity.UserContact;
import com.example.zbh.dsliao.utils.DliaoImageCache;
import com.example.zbh.dsliao.utils.DliaoUtils;
import com.example.zbh.dsliao.utils.PreferenceHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 聊天界面的 消息适配器
 */
public class ChatAdapter extends BaseAdapter {
    private ImageMessageBody imageBody;
    private List<EMMessage> list;
    private Context context;
    private AssetManager manager;
    private Typeface typeface;
    private String username;


    public ChatAdapter(Context context) {
        this.context = context;
        manager = context.getAssets();
    }

    public void addData(List<EMMessage> list, String username) {
        this.list = list;
        this.username = username;
        notifyDataSetChanged();
        Log.i("Sysout", "addData");
    }


    private boolean isLength() {
        return list != null && list.size() > 0;
    }

    @Override
    public int getCount() {
        return isLength() ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;

        String nowavatar = PreferenceHelper.getInstance().getThreeCurrentUserAvatar();
        UserContact userContact = DatabaseHelper.getInstance().queryContactPeopleByName(username);

        MyViewHolder myViewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_gut, null);
            myViewHolder = new MyViewHolder();
            myViewHolder.leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
            myViewHolder.rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
            myViewHolder.leftMsg = (TextView) view.findViewById(R.id.left_msg);
            myViewHolder.rightMsg = (TextView) view.findViewById(R.id.right_msg);
            myViewHolder.avaterLeft = (ImageView) view.findViewById(R.id.avaterLeft);
            myViewHolder.avaterRight = (ImageView) view.findViewById(R.id.avaterRight);
            myViewHolder.left_pic = (ImageView) view.findViewById(R.id.left_pic);
            myViewHolder.right_pic = (ImageView) view.findViewById(R.id.right_pic);
            myViewHolder.chatTimeTv = (TextView) view.findViewById(R.id.chatTimeTv);
            myViewHolder.chatOnelayout = (LinearLayout) view.findViewById(R.id.chatOnelayout);
            myViewHolder.voice_right = (LinearLayout) view.findViewById(R.id.voice_right);
            myViewHolder.voice_left = (LinearLayout) view.findViewById(R.id.voice_left);
            myViewHolder.voice_left_time = (TextView) view.findViewById(R.id.voice_left_time);
            myViewHolder.empy = (TextView) view.findViewById(R.id.empy);
            myViewHolder.empy_right = (TextView) view.findViewById(R.id.empy_right);
            myViewHolder.voice_right = (LinearLayout) view.findViewById(R.id.voice_right);
            myViewHolder.voice_right_time = (TextView) view.findViewById(R.id.voice_right_time);


            //设置当前用户头像
            if (nowavatar != null){
                setUserAvatar(context, nowavatar, myViewHolder.avaterRight);
            }
            //设置来消息的用户头像
            if (userContact != null){
                if (userContact.getAvatar() != null){
                    setUserAvatar(context, userContact.getAvatar(), myViewHolder.avaterLeft);
                }
            }

            myViewHolder.left_file_lin = (LinearLayout) view.findViewById(R.id.left_file_lin);
            myViewHolder.right_file_lin = (LinearLayout) view.findViewById(R.id.right_file_lin);
            myViewHolder.left_file_name = (TextView) view.findViewById(R.id.left_file_name);
            myViewHolder.right_file_name = (TextView) view.findViewById(R.id.right_file_name);
            myViewHolder.left_file_size = (TextView) view.findViewById(R.id.left_file_size);
            myViewHolder.right_file_size = (TextView) view.findViewById(R.id.right_file_size);
            myViewHolder.left_file_status = (TextView) view.findViewById(R.id.left_file_status);
            myViewHolder.right_file_status = (TextView) view.findViewById(R.id.right_file_status);
            myViewHolder.left_video_rel = (RelativeLayout) view.findViewById(R.id.left_video_rel);
            myViewHolder.right_video_rel = (RelativeLayout) view.findViewById(R.id.right_video_rel);
            myViewHolder.left_chatting_content_iv = (ImageView) view.findViewById(R.id.left_chatting_content_iv);
            myViewHolder.right_chatting_content_iv = (ImageView) view.findViewById(R.id.right_chatting_content_iv);
            myViewHolder.right_chatting_size_iv = (TextView) view.findViewById(R.id.right_chatting_size_iv);
            myViewHolder.left_chatting_size_iv = (TextView) view.findViewById(R.id.left_chatting_size_iv);
            myViewHolder.left_chatting_length_iv = (TextView) view.findViewById(R.id.left_chatting_length_iv);
            myViewHolder.right_chatting_length_iv = (TextView) view.findViewById(R.id.right_chatting_length_iv);
            myViewHolder.right_chatting_status_btn = (ImageView) view.findViewById(R.id.right_chatting_status_btn);
            myViewHolder.left_chatting_status_btn = (ImageView) view.findViewById(R.id.right_chatting_status_btn);


            view.setTag(myViewHolder);
        } else {
            view = convertView;
            myViewHolder = (MyViewHolder) view.getTag();
        }


        final EMMessage message = list.get(position);
        String nextMessageTime = null;
        //取上一条消息
        EMMessage nextMessage = null;
        if (position > 0){
            nextMessage = list.get(position - 1);
            //上一条消息的时间
            nextMessageTime = DateUtils.getTimestampString(new Date(nextMessage.getMsgTime()));
        }

        //判断消息类型 回调到activity 接收判断 弹出哪个dialog
        final int type;
        if (message.getType() == EMMessage.Type.TXT){
            type = 0;
        }else if (message.getType() == EMMessage.Type.VOICE || message.getType() == EMMessage.Type.LOCATION){
            type = 2;
        }else {
            type = 1;
        }


        final String MsgId = message.getMsgId();//得到本条消息的id(用途:删除本条消息)
        Log.d("ChatAdapter", MsgId + "<<<<<<<");




        //时间
        String messagetime = DateUtils.getTimestampString(new Date(message.getMsgTime()));
        if (nextMessageTime != null){
            if (!messagetime.equals(nextMessageTime)){
                myViewHolder.chatTimeTv.setVisibility(View.VISIBLE);
                myViewHolder.chatTimeTv.setText(messagetime);
                nextMessageTime = null;
            }
        }


        //得到文本信息
        if (message.direct == EMMessage.Direct.RECEIVE) {
            myViewHolder.avaterLeft.setVisibility(View.VISIBLE);
            myViewHolder.avaterRight.setVisibility(View.GONE);
            myViewHolder.leftLayout.setVisibility(View.VISIBLE);
            myViewHolder.rightLayout.setVisibility(View.GONE);
            myViewHolder.leftMsg.setVisibility(View.GONE);
            myViewHolder.left_pic.setVisibility(View.GONE);
            myViewHolder.voice_right.setVisibility(View.GONE);
            myViewHolder.voice_left.setVisibility(View.GONE);
            myViewHolder.left_file_lin.setVisibility(View.GONE);
            myViewHolder.right_file_lin.setVisibility(View.GONE);
            myViewHolder.left_video_rel.setVisibility(View.GONE);
            myViewHolder.right_video_rel.setVisibility(View.GONE);

            switch (message.getType()) {
                case TXT:
                    TextMessageBody body = (TextMessageBody) message.getBody();
                    String text = body.getMessage();
                    CharSequence cs = Html.fromHtml(text, imageGetter, null);
                    setHolderText(myViewHolder.leftMsg);
                    myViewHolder.leftMsg.setVisibility(View.VISIBLE);
                    myViewHolder.leftMsg.setText(cs);
                    break;
                case IMAGE:
                    myViewHolder.left_pic.setVisibility(View.VISIBLE);
                    setUpPic(message, myViewHolder.left_pic);
                    myViewHolder.left_pic.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (message != null){
                                onImageClickListener.onImageClick(username, message);
                            }
                        }
                    });
                    break;
                case VIDEO:
                    myViewHolder.left_video_rel.setVisibility(View.VISIBLE);
                    VideoMessageBody videoMessageBody = (VideoMessageBody) message.getBody();
                    String localThumb = videoMessageBody.getLocalThumb();
                    if (localThumb != null) {
                        showVideoThumbView(localThumb, myViewHolder.left_chatting_content_iv, videoMessageBody.getThumbnailUrl(), message);
                    }
                    if (videoMessageBody.getLength() > 0){
                        String videoTime = DateUtils.toTimeBySecond(videoMessageBody.getLength());
                        myViewHolder.left_chatting_length_iv.setText(videoTime);
                    }
                    if (videoMessageBody.getVideoFileLength() > 0){
                        String videoSize = TextFormater.getDataSize(videoMessageBody.getVideoFileLength());
                        myViewHolder.left_chatting_size_iv.setText(videoSize);
                    }
                    if (message.status == EMMessage.Status.INPROGRESS){
                        myViewHolder.left_chatting_content_iv.setImageResource(R.mipmap.dl_default_image);
                    }else {
                        myViewHolder.left_chatting_content_iv.setImageResource(R.mipmap.dl_default_image);
                        showVideoThumbView(localThumb,myViewHolder.left_chatting_content_iv,videoMessageBody.getThumbnailUrl(),message);
                    }
                    myViewHolder.left_video_rel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (videoClickListener != null){
                                videoClickListener.onShowVideoClick(message);
                            }
                        }
                    });
                    break;
                case LOCATION:
                    break;
                case VOICE:
                    myViewHolder.voice_left.setVisibility(View.VISIBLE);
                    VoiceMessageBody voiceMessageBody = (VoiceMessageBody) message.getBody();
                    final String fileName = voiceMessageBody.getLocalUrl();
                    int voicetime = (voiceMessageBody.getLength()) / 2000;
                    myViewHolder.voice_left_time.setText(voicetime + "");
                    switch (voicetime){
                        case 1:
                            myViewHolder.empy.setText(" ");
                            break;
                        case 3:
                            myViewHolder.empy.setText("     ");
                            break;
                        case 5:
                            myViewHolder.empy.setText("         ");
                            break;
                        case 7:
                            myViewHolder.empy.setText("           ");
                            break;
                        case 9:
                            myViewHolder.empy.setText("               ");
                            break;
                        case 10-60:
                            myViewHolder.empy.setText("                 ");
                            break;
                    }
                    myViewHolder.voice_left.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (voiceListener != null){
                                voiceListener.OnVoiceClick(fileName);
                            }
                        }
                    });
                    break;
                case FILE:
                    myViewHolder.left_file_lin.setVisibility(View.VISIBLE);
                    NormalFileMessageBody fileMessageBody = (NormalFileMessageBody) message.getBody();
                    String filePath = fileMessageBody.getLocalUrl();
                    //赋值
                    myViewHolder.left_file_name.setText(fileMessageBody.getFileName());
                    myViewHolder.left_file_size.setText(TextFormater.getDataSize(fileMessageBody.getFileSize()));
                    File file = new File(filePath);
                    if (file != null && file.exists()) {
                        myViewHolder.left_file_status.setText("已下载");
                    } else {
                        myViewHolder.left_file_status.setText("未下载");
                    }
                    myViewHolder.left_file_lin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (fileListener != null) {
                                fileListener.onFileClick(message);
                            }

                        }
                    });

                    break;
                case CMD:
                    break;
            }

        } else {
            myViewHolder.avaterRight.setVisibility(View.VISIBLE);
            myViewHolder.avaterLeft.setVisibility(View.GONE);
            myViewHolder.leftLayout.setVisibility(View.GONE);
            myViewHolder.rightLayout.setVisibility(View.VISIBLE);
            myViewHolder.rightMsg.setVisibility(View.GONE);
            myViewHolder.right_pic.setVisibility(View.GONE);
            myViewHolder.voice_right.setVisibility(View.GONE);
            myViewHolder.voice_left.setVisibility(View.GONE);
            myViewHolder.left_file_lin.setVisibility(View.GONE);
            myViewHolder.right_file_lin.setVisibility(View.GONE);
            myViewHolder.left_video_rel.setVisibility(View.GONE);
            myViewHolder.right_video_rel.setVisibility(View.GONE);
            switch (message.getType()) {
                case TXT:
                    TextMessageBody body = (TextMessageBody) message.getBody();
                    String text = body.getMessage();
                    CharSequence cs = Html.fromHtml(text, imageGetter, null);
                    setHolderText(myViewHolder.rightMsg);
                    myViewHolder.rightMsg.setVisibility(View.VISIBLE);
                    myViewHolder.rightMsg.setText(cs);
                    break;
                case IMAGE:
                    myViewHolder.right_pic.setVisibility(View.VISIBLE);
                    setUpPic(message, myViewHolder.right_pic);
                    //图片类型消息的点击事件 将本条message回调给activity
                    myViewHolder.right_pic.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (message != null){
                                onImageClickListener.onImageClick(username, message);
                            }
                        }
                    });
                    break;
                case VIDEO:
                    myViewHolder.right_video_rel.setVisibility(View.VISIBLE);
                    VideoMessageBody videoMessageBody = (VideoMessageBody) message.getBody();
                    String localThumb = videoMessageBody.getLocalThumb();
                    if (localThumb != null) {
                        showVideoThumbView(localThumb, myViewHolder.right_chatting_content_iv, videoMessageBody.getThumbnailUrl(), message);
                    }
                    if (videoMessageBody.getLength() > 0){
                        String videoTime = DateUtils.toTimeBySecond(videoMessageBody.getLength());
                        myViewHolder.right_chatting_length_iv.setText(videoTime);
                    }
                    if (videoMessageBody.getLocalUrl() != null && new File(videoMessageBody.getLocalUrl()).exists()){
                        String videoSize = TextFormater.getDataSize(videoMessageBody.getVideoFileLength());
                        myViewHolder.right_chatting_size_iv.setText(videoSize);
                    }
                    myViewHolder.right_video_rel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (videoClickListener != null){
                                videoClickListener.onShowVideoClick(message);
                            }
                        }
                    });
                    break;
                case LOCATION:
                    break;
                case VOICE:
                    myViewHolder.voice_right.setVisibility(View.VISIBLE);
                    VoiceMessageBody voiceMessageBody = (VoiceMessageBody) message.getBody();
                    int voicetimeright = (voiceMessageBody.getLength()) / 2000;
                    myViewHolder.voice_right_time.setText(voicetimeright + "");
                    final  String fileName = voiceMessageBody.getLocalUrl();
                    final int len = voiceMessageBody.getLength();
                    myViewHolder.voice_right.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (voiceListener != null){
                                Log.i("sssddddd", len + "");
                                voiceListener.OnVoiceClick(fileName);
                            }
                        }
                    });
                    break;
                case FILE:
                    myViewHolder.right_file_lin.setVisibility(View.VISIBLE);
                    NormalFileMessageBody fileMessageBody = (NormalFileMessageBody) message.getBody();
                    String filePath = fileMessageBody.getLocalUrl();
                    //赋值
                    myViewHolder.right_file_name.setText(fileMessageBody.getFileName());
                    myViewHolder.right_file_size.setText(TextFormater.getDataSize(fileMessageBody.getFileSize()));
                    myViewHolder.right_file_lin.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (fileListener != null) {
                                fileListener.onFileClick(message);
                            }
                        }
                    });


                    break;
                case CMD:
                    break;
            }


        }

        //长按一整条消息的行布局
        myViewHolder.chatOnelayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemChatOneLongClickListener != null) {
                    onItemChatOneLongClickListener.OnChatOneLongClick(v, MsgId, type);
                }
                return true;
            }
        });

        return view;
    }

    /**
     * 设置头像
     * **/
    private void setUserAvatar(Context context, String avater, ImageView userImg) {
        if (avater != null){
            Glide.with(context).load(avater).into(userImg);
            Log.d("wonima", "wonima" + ">>>>>");
        }
    }

    /***
     * 设置聊天字体属性
     * */
    private void setHolderText(TextView holderText) {
        String textType = PreferenceHelper.getInstance().getCurrentTextType();
        String textColor = PreferenceHelper.getInstance().getCurrentuserTextColor();
        String textSize = PreferenceHelper.getInstance().getCurrentTextSize();
        if (textType != null){
            switch (textType){
                case Constant.TYPE_01:
                    typeface = Typeface.createFromAsset(manager, "xindixiaowanzi.ttf");
                    holderText.setTypeface(typeface);
                    break;
                case Constant.TYPE_02:
                    typeface = Typeface.createFromAsset(manager,"xindiwenguimingti.ttf");
                    holderText.setTypeface(typeface);
                    break;
                case Constant.TYPE_03:
                    typeface = Typeface.DEFAULT;
                    holderText.setTypeface(typeface);
                    break;
            }
        }
        if (textColor != null){
            switch (textColor){
                case Constant.COLOR_01:
                    int colorb = Constant.COLOR_BLACK;
                    holderText.setTextColor(colorb);
                    break;
                case Constant.COLOR_02:
                    int colorw = Constant.COLOR_WHITE;
                    holderText.setTextColor(colorw);
                    break;
                case Constant.COLOR_03:
                    int colorr = Constant.COLOR_GREEN;
                    holderText.setTextColor(colorr);
                    break;
                case Constant.COLOR_04:
                    int colorg = Constant.COLOR_RED;
                    holderText.setTextColor(colorg);
                    break;
            }
        }
        if (textSize != null){
            switch (textSize){
                case Constant.SIZE_01:
                    float l = Constant.SIZE_LARGE;
                    holderText.setTextSize(l);
                    break;
                case Constant.SIZE_02:
                    float m = Constant.SIZE_MIDDLE;
                    holderText.setTextSize(m);
                    break;
                case Constant.SIZE_03:
                    float s = Constant.SIZE_SMALL;
                    holderText.setTextSize(s);
                    break;
            }
        }

    }



    /**
     * 时间格式转换
     * **/
    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }


    class MyViewHolder {
        LinearLayout rightLayout, leftLayout, right_file_lin, left_file_lin,voice_right, voice_left;
        TextView chatTimeTv, leftMsg, rightMsg, right_file_name, left_file_name,
                left_file_size, right_file_size, right_file_status, left_file_status, right_chatting_size_iv,
                left_chatting_size_iv, right_chatting_length_iv, left_chatting_length_iv, voice_left_time, empy, empy_right, voice_right_time;//时间
        LinearLayout chatOnelayout;//一条会话的整体布局
        ImageView avaterLeft, avaterRight,left_pic ,right_pic, right_chatting_content_iv, left_chatting_content_iv,
                left_chatting_status_btn, right_chatting_status_btn;
        RelativeLayout right_video_rel,left_video_rel;
    }

    //设置图片信息
    public void setUpPic(EMMessage message, ImageView imageView) {
        imageBody = (ImageMessageBody) message.getBody();
        if (message.direct == EMMessage.Direct.RECEIVE) {
            if (message.status == EMMessage.Status.INPROGRESS) {
                imageView.setImageResource(R.mipmap.ic_launcher);
            } else {
                imageView.setImageResource(R.mipmap.ic_launcher);
                if (imageBody.getLocalUrl() != null) {
                    // String filePath = imgBody.getLocalUrl();
                    String remotePath = imageBody.getRemoteUrl();
                    String filePath = DliaoUtils.getImagePath(remotePath);
                    String thumbRemoteUrl = imageBody.getThumbnailUrl();
                    String thumbnailPath = DliaoUtils.getThumbnailImagePath(thumbRemoteUrl);
                    showImageView(thumbnailPath, imageView, filePath, message);
                }
            }
            return;
        }
        String filePath = imageBody.getLocalUrl();
        if (filePath != null) {
            showImageView(DliaoUtils.getThumbnailImagePath(filePath), imageView, filePath, message);
        }

    }
    /**
     * 展示视频缩略图
     *
     * @param localThumb   本地缩略图路径
     * @param iv
     * @param thumbnailUrl 远程缩略图路径
     * @param message
     */
    private void showVideoThumbView(final String localThumb, final ImageView iv, String thumbnailUrl, final EMMessage message) {
        // first check if the thumbnail image already loaded into cache
        Bitmap bitmap = DliaoImageCache.getInstance().get(localThumb);
        if (bitmap != null) {
            // thumbnail image is already loaded, reuse the drawable
            iv.setImageBitmap(bitmap);

        } else {
            new AsyncTask<Void, Void, Bitmap>() {

                @Override
                protected Bitmap doInBackground(Void... params) {
                    if (new File(localThumb).exists()) {
                        return ImageUtils.decodeScaleImage(localThumb, 160, 160);
                    } else {
                        return null;
                    }
                }

                @Override
                protected void onPostExecute(Bitmap result) {
                    super.onPostExecute(result);
                    if (result != null) {
                        DliaoImageCache.getInstance().put(localThumb, result);
                        iv.setImageBitmap(result);

                    } else {
                        if (message.status == EMMessage.Status.FAIL) {
                            if (DliaoUtils.isNetWorkConnected(context)) {
                                EMChatManager.getInstance().asyncFetchMessage(message);
                            }
                        }

                    }
                }
            }.execute();
        }

    }


    //展示
    private boolean showImageView(final String thumbernailPath, final ImageView iv, final String localFullSizePath, final EMMessage message) {
        // first check if the thumbnail image already loaded into cache
        Bitmap bitmap = DliaoImageCache.getInstance().get(thumbernailPath);
        if (bitmap != null) {
            // thumbnail image is already loaded, reuse the drawable
            iv.setImageBitmap(bitmap);
            return true;
        } else {
            new AsyncTask<Object, Void, Bitmap>() {

                @Override
                protected Bitmap doInBackground(Object... args) {
                    File file = new File(thumbernailPath);
                    if (file.exists()) {
                        return DliaoUtils.decodeScaleImage(thumbernailPath, 160, 160);
                    } else {
                        if (message.direct == EMMessage.Direct.SEND) {
                            return DliaoUtils.decodeScaleImage(localFullSizePath, 160, 160);
                        } else {
                            return null;
                        }
                    }
                }

                protected void onPostExecute(Bitmap image) {
                    if (image != null) {
                        iv.setImageBitmap(image);
                        DliaoImageCache.getInstance().put(thumbernailPath, image);
                    } else {
                        if (message.status == EMMessage.Status.FAIL) {
                            if (DliaoUtils.isNetWorkConnected(App.getContext())) {
                                new Thread(new Runnable() {

                                    @Override
                                    public void run() {
                                        EMChatManager.getInstance().asyncFetchMessage(message);
                                    }
                                }).start();
                            }
                        }

                    }
                }
            }.execute();

            return true;
        }
    }
    private Html.ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            int id = Integer.parseInt(source);
            Drawable drawable = context.getResources().getDrawable(id);
            drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
            return drawable;
        }
    };


    /**
     * 长按一条消息的接口毁掉
     * **/
    private OnItemChatOneLongClickListener onItemChatOneLongClickListener;
    public void setOnItemChatOneLongClickListener(OnItemChatOneLongClickListener onItemChatOneLongClickListener){
        this.onItemChatOneLongClickListener = onItemChatOneLongClickListener;
    }
    public interface OnItemChatOneLongClickListener{
        void OnChatOneLongClick(View view, String MsgId, int type);
    }
    //语音
    public interface OnVoiceClickListener{
        void OnVoiceClick(String name);
    }
    private OnVoiceClickListener voiceListener;
    public void setOnVoiceClickListener(OnVoiceClickListener voiceListener){
        this.voiceListener = voiceListener;
    }
    //下载文件
    public interface OnFileClickListener {
        void onFileClick(EMMessage message);
    }

    private OnFileClickListener fileListener;

    public void setOnFileClickListener(OnFileClickListener fileListener) {
        this.fileListener = fileListener;
    }
    public interface OnShowVideoClickListener{
        void onShowVideoClick(EMMessage message);
    }
    private OnShowVideoClickListener videoClickListener;
    public void setOnShowVideoClickListener(OnShowVideoClickListener videoClickListener){
        this.videoClickListener = videoClickListener;
    }

    //放大图片
    public interface OnImageClickListener{
        void onImageClick(String username, EMMessage message);
    }
    private OnImageClickListener onImageClickListener;

    public void setOnImageClickListener(OnImageClickListener onImageClickListener){
        this.onImageClickListener = onImageClickListener;
    }


}
